package com.suanfa.collect;

/**
 * 给定一个数组由一个有序数组经过多次旋转而来，每次旋转修改一个元素
 */
public class RotateArray {

    public static void main(String[] args) {
        int[] arr={2,1};
        System.out.println(getMinIndex(arr));
    }


    public static int getMinIndex(int[] arr){
        if(arr.length < 1){
            return Integer.MIN_VALUE;
        }
        int min = arr[0];
        int s = 0;
        int e = arr.length;

        while(s < e){
            int mid = (s+e) >> 1;
            //当中间值小于起始值,说明中点在后半段，往前挪动
            if(arr[mid] <= min){
                e--;
                min = arr[mid];
            }else {
                //中间值大于起始值，证明终点在前半段，
                s++;
            }
        }
        return min;
    }

    public static int findPeakElement(int[] arr){
        if(arr.length < 3){
            return -1;
        }
        int s = 0;
        int e = arr.length;
        while(s < e){
            int mid = (s+e) >> 1;

            if(arr[mid] < arr[mid +1]){
                e = mid;
            }else{
                s = mid + 1;
            }
        }
        return s;

    }
}
